﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Net.Http;
using System.Net.Http.Headers;
using System.Net;
using System.IO;

namespace wd_8_http_get
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        static string get_url1 = "http://www.fanyunedu.com/mms/login.html";
        static string my1 = "";
        //static string error1 = "";
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Button_Click_GET_ASYNC(object sender, RoutedEventArgs e)
        {
            GetRequestAsync(get_url1);
        }

        async static void GetRequestAsync(string url)
        {
            using (HttpClient client1 = new HttpClient())
            {
                using (HttpResponseMessage response1 = await client1.GetAsync(url))
                {
                    
                    using (HttpContent content1 = response1.Content)
                    {
                        my1 = await content1.ReadAsStringAsync();
                        //MessageBox.Show(my1);
                        HttpContentHeaders headers = content1.Headers;
                        MessageBox.Show(headers.ToString());
                    }
                }
            }
        }

        static string GetRequest(string url)
        {
            string ret = "";
            //https://blog.csdn.net/qq_40649350/article/details/118682995
            HttpWebRequest request1 = null;
            HttpWebResponse response1 = null;
            //StreamReader reader1 = null;
                        
            try
            {
                request1 = (HttpWebRequest)WebRequest.Create(get_url1);
                request1.Method = "GET";

                response1 = (HttpWebResponse)request1.GetResponse();
                //using(reader1 = new StreamReader(response1.GetResponseStream()))
                //{
                //    ret = reader1.ReadToEnd();//all content                    
                //}

                ret = response1.Headers.ToString();

            }
            catch (System.Exception ex)
            {
                ret = ex.Message;
            }

            return ret;
        }

        private void Button_Click_GET(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(GetRequest(get_url1));
        }
    }
}
